/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-12-03     yoyoyes      first version
 */

#include <thread_semaphore_demo1.h>

static rt_sem_t dynamic_sem = RT_NULL;      /* 指向信号量的指针 */

ALIGN(RT_ALIGN_SIZE)

static char thread1_stack[1024];
static struct rt_thread thread1;

/*
 * @brief  线程1入口函数
 *
 * @param  parameter 线程1入口函数参数
 *
 * @return None
 */
static void rt_thread1_entry(void *parameter)
{
    static rt_uint8_t count = 0;

    while(1)
    {
        if(count <= 100)
            count++;
        else
        {
            rt_kprintf("exit.\n");
            return;
        }

        if(0 == (count % 10))
        {
            rt_kprintf("thread1 release a dynamic semaphore.\n");
            rt_sem_release(dynamic_sem);
        }
    }
}

ALIGN(RT_ALIGN_SIZE)

static char thread2_stack[1024];
static struct rt_thread thread2;

/*
 * @brief  线程2入口函数
 *
 * @param  parameter 线程2入口函数参数
 *
 * @return None
 */
static void rt_thread2_entry(void *parameter)
{
    static rt_err_t result;
    static rt_uint8_t number = 0;

    while(1)
    {
        result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER);

        if(result != RT_NULL)
        {
            rt_kprintf("thread2 take a dynamic semaphore, failed.\n");
            rt_sem_delete(dynamic_sem);
            return;
        }
        else
        {
            number++;
            rt_kprintf("thread2 take a dynamic semaphore, value = %d\n", number);
        }
    }
}

/*
 * @brief  信号量Demo函数
 *
 * @param  None
 *
 * @return None
 */
int thread_semaphore_demo1(void)
{
    /* 创建一个动态信号量 */
    dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO);
    if(dynamic_sem == RT_NULL)
    {
        rt_kprintf("create dynamic semaphore failed.\n");
        return -1;
    }
    else
    {
        rt_kprintf("create done.dynamic semaphore value = 0.\n");
    }

    /* 初始化线程1 */
    rt_thread_init(&thread1, "thread1", rt_thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), THREAD_PRIORITY, THREAD_TIMESLICE);
    rt_thread_startup(&thread1);

    /* 初始化线程2 */
    rt_thread_init(&thread2, "thread2", rt_thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    rt_thread_startup(&thread2);

    return 0;
}











